package com.example.controller;

import com.example.model.Student;
import com.example.service.StudentService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import jakarta.validation.Valid;
import java.util.List;
import java.util.Optional;

/**
 * 学生控制器
 * 提供学生信息的CRUD操作API
 */
@Slf4j
@RestController
@RequestMapping("/api/students")
@RequiredArgsConstructor
public class StudentController {
    
    private final StudentService studentService;
    
    /**
     * 创建学生
     */
    @PostMapping
    public ResponseEntity<Student> createStudent(@Valid @RequestBody Student student) {
        log.info("创建学生请求: {}", student.getName());
        Student savedStudent = studentService.saveStudent(student);
        return ResponseEntity.status(HttpStatus.CREATED).body(savedStudent);
    }
    
    /**
     * 根据ID获取学生信息
     */
    @GetMapping("/{id}")
    public ResponseEntity<Student> getStudentById(@PathVariable String id) {
        log.info("获取学生信息请求: {}", id);
        Optional<Student> student = studentService.findById(id);
        return student.map(s -> ResponseEntity.ok(s))
                     .orElse(ResponseEntity.notFound().build());
    }
    
    /**
     * 获取所有学生（分页）
     */
    @GetMapping
    public ResponseEntity<Page<Student>> getAllStudents(
            @RequestParam(defaultValue = "0") int page,
            @RequestParam(defaultValue = "10") int size) {
        log.info("获取所有学生请求，页码: {}, 大小: {}", page, size);
        Pageable pageable = PageRequest.of(page, size);
        Page<Student> students = studentService.findAllStudents(pageable);
        return ResponseEntity.ok(students);
    }
    
    /**
     * 根据姓名搜索学生
     */
    @GetMapping("/search/name")
    public ResponseEntity<List<Student>> searchByName(@RequestParam String name) {
        log.info("根据姓名搜索学生: {}", name);
        List<Student> students = studentService.searchByName(name);
        return ResponseEntity.ok(students);
    }
    
    /**
     * 根据年龄范围查找学生
     */
    @GetMapping("/search/age")
    public ResponseEntity<List<Student>> findByAgeRange(
            @RequestParam Integer minAge,
            @RequestParam Integer maxAge) {
        log.info("根据年龄范围查找学生: {} - {}", minAge, maxAge);
        List<Student> students = studentService.findByAgeRange(minAge, maxAge);
        return ResponseEntity.ok(students);
    }
    
    /**
     * 根据学号查找学生
     */
    @GetMapping("/search/number")
    public ResponseEntity<Student> findByNumber(@RequestParam String number) {
        log.info("根据学号查找学生: {}", number);
        Student student = studentService.findByNumber(number);
        return student != null ? ResponseEntity.ok(student) : ResponseEntity.notFound().build();
    }
    
    /**
     * 根据父亲姓名查找学生
     */
    @GetMapping("/search/father")
    public ResponseEntity<List<Student>> findByFatherName(@RequestParam String fatherName) {
        log.info("根据父亲姓名查找学生: {}", fatherName);
        List<Student> students = studentService.findByFatherName(fatherName);
        return ResponseEntity.ok(students);
    }
    
    /**
     * 根据母亲姓名查找学生
     */
    @GetMapping("/search/mother")
    public ResponseEntity<List<Student>> findByMotherName(@RequestParam String motherName) {
        log.info("根据母亲姓名查找学生: {}", motherName);
        List<Student> students = studentService.findByMotherName(motherName);
        return ResponseEntity.ok(students);
    }
    
    /**
     * 更新学生信息
     */
    @PutMapping("/{id}")
    public ResponseEntity<Student> updateStudent(
            @PathVariable String id,
            @Valid @RequestBody Student student) {
        log.info("更新学生信息请求: {}", id);
        student.setId(id);
        Student updatedStudent = studentService.updateStudent(student);
        return ResponseEntity.ok(updatedStudent);
    }
    
    /**
     * 删除学生
     */
    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteStudent(@PathVariable String id) {
        log.info("删除学生请求: {}", id);
        studentService.deleteStudent(id);
        return ResponseEntity.noContent().build();
    }
    
    /**
     * 获取学生总数
     */
    @GetMapping("/count")
    public ResponseEntity<Long> getStudentCount() {
        log.info("获取学生总数请求");
        long count = studentService.countStudents();
        return ResponseEntity.ok(count);
    }
}